﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using System.Data;
using System.IO;
using System.Drawing;
public partial class administrator_uc_admin_e_customer : WebPageSecurity
{
    protected void Page_Load(object sender, EventArgs e)
    {        
        if (!IsPostBack)
        {
            PanelUpdatePass.Visible = false;
            if (!string.IsNullOrEmpty(Request.QueryString["itemid"]))
            {                
                LoadDataInfo(Request.QueryString["itemid"].ToString());                
            }
        }
    }   
    private void LoadDataInfo(string ItemID)
    {
        DataTable dt = new BLL.KhachHang_BLL().GetCustomerById(new Guid(ItemID));
        if (dt.Rows.Count > 0)
        {
            hdItemID.Value = dt.Rows[0]["CustommerID"] + "";
            txtUserName.Text = dt.Rows[0]["LoginName"].ToString();
            txtEmail.Text = dt.Rows[0]["Email"].ToString();
            txtFullName.Text = dt.Rows[0]["FullName"].ToString();
            txtAddress.Text = dt.Rows[0]["Address"].ToString();
            DropDownListDay.SelectedValue = Convert.ToDateTime(dt.Rows[0]["BirthDay"]).Day.ToString();
            DropDownListMonth.SelectedValue = Convert.ToDateTime(dt.Rows[0]["BirthDay"]).Month.ToString();
            txtYear.Text = Convert.ToDateTime(dt.Rows[0]["BirthDay"]).Year.ToString();
            rdSex.SelectedValue = dt.Rows[0]["_Sex"].ToString();
            rdActive.SelectedValue = dt.Rows[0]["Active"] + "";
            txtLockedReason.Text = dt.Rows[0]["LockedReason"].ToString(); 
            ltCountLogin.Text = "";
            ltCreated.Text = dt.Rows[0]["Created"].ToString();
            ltLastLogin.Text = "";
            txt_Phone.Text = dt.Rows[0]["PhoneNumber"].ToString();
            
        }
        else
            lblMessage.Text = "Load data failt!";
    }    
    protected void lbtEditItem_Click(object sender, EventArgs e)
    {
        try
        {
            DateTime BirthDay = new DateTime();
            if (!string.IsNullOrEmpty(txtYear.Text))
                BirthDay = new DateTime(int.Parse("0" + txtYear.Text), int.Parse("0" + DropDownListMonth.SelectedValue), int.Parse("0" + DropDownListDay.SelectedValue));
            else BirthDay = DateTime.Now;

            string GioiTinh = "Nu";

            if (rdSex.SelectedValue.ToString().Equals("1")) {
                GioiTinh = "Nam";
            }
                
            bool resetPass = false;
            string Password = string.Empty;
            if (chkUpdatePass.Checked)
            {
                resetPass = true;
                if (string.IsNullOrEmpty(txtPassword.Text.Trim()))
                {
                    lblmessagePass.Text = "(*)";
                    return;
                }
                if (!txtPassword.Text.Trim().Equals(txtRePassword.Text.Trim()))
                {
                    lblmessageRePass.Text = "Xác nhận pass lỗi!";
                    return;
                }
                lblmessagePass.Text = "";
                lblmessageRePass.Text = "";
                Password = SecurityServices.GetMD5(txtPassword.Text.Trim());
            }
            
                //new CmsBLL().Manager_UpDateItem(new Guid(hdItemID.Value), "", txtEmail.Text, txtFullName.Text, txtAddress.Text, BirthDay, int.Parse("0" + rdSex.SelectedValue), int.Parse("0" + rdActive.SelectedValue), txtLockedReason.Text);                
                new KhachHang_BLL().Admin_UpdateCustomer(hdItemID.Value, txtFullName.Text, txt_Phone.Text, txtAddress.Text, GioiTinh, BirthDay, int.Parse("0" + rdActive.SelectedValue), txtLockedReason.Text,txtEmail.Text,resetPass,Password);
            
            lblMessage.Text = "Cập nhật thành công!";
        }
        catch (Exception ex)
        {
            lblMessage.Text = "ERROR! " + ex.Message.ToString();
        }
    }
    protected void chkUpdatePass_CheckedChanged(object sender, EventArgs e)
    {
        if (chkUpdatePass.Checked)
            PanelUpdatePass.Visible = true;
        else
            PanelUpdatePass.Visible = false;
    }
}